var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pname:'',
    stars:[],
    catagory:'',
    pid:'',
    starNum:5,
    notes:'',
    action:'add'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;

    // 分类
    let catagory = options.catagory;

    // 产品id
    let pid = options.pid;

    wx.request({
      url: app.globalData.remoteDomainApi+'getpname.php',
      data: {
        catagory: catagory,
        pid: pid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data)
        //产品名称
        that.setData({
          pname:res.data.pname,
          catagory: catagory,
          pid: pid,
        })
      }
    })


    //星星
    var tempArr = [];
    for(let i = 1 ;i<6;i++){

      if(!options.stars){
        let starUrl = '../../static/ico/star-gray.png';
        tempArr.push({ id: i, starUrl: starUrl })
      }else{
        //红星
        let redStar = '../../static/ico/star.png'

        //灰星
        let grayStar = '../../static/ico/star-gray.png'

        if (i <= options.stars) {
          tempArr.push({ id: i, starUrl: redStar })
        } else {
          tempArr.push({ id: i, starUrl: grayStar })
        }
      }
     
    }

    this.setData({
      stars:tempArr,
      notes:options.notes
    })

    if(options.action){
      this.setData({
        action: options.action
      })
    }
   
  },
  /**
   * 选择星星
   */
  selectStar:function(e){
    var tempArr = [];
    let starNum = e.currentTarget.id

    //重置stars的内容
    for(let i=1;i<6;i++){
      //红星
      let redStar = '../../static/ico/star.png'

      //灰星
      let grayStar = '../../static/ico/star-gray.png'

      if(i <= starNum){
        tempArr.push({id: i, starUrl:redStar})
      }else{
        tempArr.push({id: i, starUrl:grayStar})
      }
    }
    this.setData({
      stars: tempArr,
      starNum: starNum
    })
  },

  /**
   * 提交评论
   */
  formSubmit:function(e){

    var that = this;

    //分类
    let catagory = this.data.catagory;

    // 产品id
    let pid = this.data.pid;

    // openid
    let openid = app.globalData.openid;
    // 星星数
    let starnums = this.data.starNum;

    // 评论内容
    let content = e.detail.value.notes;

    //提交数据
    wx.request({
      url: app.globalData.remoteDomainApi+'comment.php',
      data: {
        action: that.data.action,
        catagory:catagory,
        pid:pid,
        openid:openid,
        starnums: starnums,
        content: content
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if(res.data == 2){
          wx.showToast({
            title: '发表成功',
            icon:'success',
            duration:2000,
            success(res){
              wx.redirectTo({
                url: '../myorder/myorder',
              })
            }
          })
        }else{
          wx.showToast({
            title: '抱歉，发表失败！',
            duration: 2000
          })
        }
      }
    })

  }
})